package com.xyazm.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyazm.coupon.domain.param.resource.AddCouponResourceParam;
import com.xyazm.coupon.domain.param.resource.ListCouponResourceParam;
import com.xyazm.coupon.domain.param.resource.UpdateCouponResourceParam;
import com.xyazm.coupon.entity.CouponResourceDO;
import com.xyazm.coupon.mapper.CouponResourceMapper;
import com.xyazm.coupon.mapstruct.CouponResourceMapstruct;
import com.xyazm.coupon.service.ICouponResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xuyou
 * @since 2021-08-09
 */
@Service
public class CouponResourceServiceImpl extends ServiceImpl<CouponResourceMapper, CouponResourceDO> implements ICouponResourceService {

    @Autowired
    private CouponResourceMapper couponResourceMapper;

    @Override
    public Page<CouponResourceDO> list(ListCouponResourceParam param) {
        QueryWrapper<CouponResourceDO> query = new QueryWrapper<>();
        query.eq(param.getCouponResourceId() != null,"coupon_resource_id",param.getCouponResourceId());
        query.like(param.getCouponResourceName() != null,"coupon_resource_name",param.getCouponResourceName());
        query.eq(param.getCouponResourceStatus() != null,"coupon_resource_status",param.getCouponResourceStatus());
        Page<CouponResourceDO> page = couponResourceMapper.selectPage(new Page<>(param.getPageNum(), param.getPageSize()), query);
        return page;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(AddCouponResourceParam param) {
        CouponResourceDO couponResource = CouponResourceMapstruct.INSTANCE.addCouponResourceParamToPO(param);
        couponResourceMapper.insert(couponResource);
        return couponResource.getCouponResourceId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UpdateCouponResourceParam param) {
        CouponResourceDO couponResource = CouponResourceMapstruct.INSTANCE.updateCouponResourceParamToPO(param);
        UpdateWrapper<CouponResourceDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CouponResourceDO::getCouponResourceId,param.getCouponResourceId());
        couponResourceMapper.update(couponResource,updateWrapper);
    }
}
